package com.tencent.common;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTools {
	/*
	 * 根据HQL语句，获得查找总记录数的HQL语句
	 */
	public static String getCountHQL(String hql) throws RuntimeException {
		
		int index = hql.indexOf("from");
		int order = hql.indexOf("order by");
		int group = hql.indexOf("group by");
		if (index != -1) {
			if (group > 0) {
				return "select count(*) " + hql.substring(index, group); 
			} else if (order > 0) {
				return "select count(*) " + hql.substring(index, order);
			}
			return "select count(*) " + hql.substring(index);
		} else {
			throw new RuntimeException("无效的HQL查询语句");
		}
	}
	
	/**
	 * 读取Map集合中的键和值
	 * @param hm
	 * @return
	 * @throws Exception
	 */
	public static String getParamsStr(HashMap<String, Object> hm) throws Exception{
		
		if (null != hm && !hm.isEmpty()) {
			StringBuffer sb = new StringBuffer();
			Iterator<String> iterator = hm.keySet().iterator();
			while (iterator.hasNext()) {

				String key = iterator.next().toString();
				sb.append(key + hm.get(key) + ",");
			}
			return sb.toString();
		}else{
			
			return "";
		}
	}
	
	/**
	 * 生成N位随机数字
	 * 
	 * @return
	 */
	public static long getSerialNumber(Integer length) throws NullPointerException {

		Double dob = Math.random();
		String str = dob.toString();
		str = str.substring(str.indexOf(".") + 1);
		if (str.indexOf("0") == 0) {
			int first = 0;
			while (true) {
				first = (int) (Math.random() * 10);
				if (first > 0) {
					break;
				}
			}
			str = str.replaceFirst("0", first + "");
		}
		str = str.substring(0, length);

		return Long.valueOf(str);
	}
	
	/**
	 * 验证电话号码
	 * 
	 * @param phoneNumber
	 * @return
	 * @throws Exception
	 */
	public static boolean validatePhoneNumber(String phoneNumber)
			throws Exception {

		try {

			if (!phoneNumber.startsWith("1") || phoneNumber.length() != 11) {
				return false;
			}

			Pattern dx = null, lt = null, yd = null;
			Matcher d = null, l = null, y = null;
			dx = Pattern.compile("^1[3578][01379]\\d{8}$");
			lt = Pattern.compile("^1[34578][01256]\\d{8}$");
			yd = Pattern
					.compile("^(134[012345678]\\d{7}|1[34578][0123456789]\\d{8})$");
			String tel = phoneNumber;
			System.out.println("tel:" + tel);
			d = dx.matcher(tel);
			l = lt.matcher(tel);
			y = yd.matcher(tel);
			if (d.matches() || l.matches() || y.matches()) {
				return true;
			} else {
				return false;
			}

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	/**
	 * 字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) throws NullPointerException {

		if (null == str || str.isEmpty() || "".equals(str.trim())
				|| 0 >= str.trim().length()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 补零
	 * @param ran
	 * @return
	 */
	public static final String getZero(Integer num)throws NullPointerException {
		
		if(null == num)
			throw new NullPointerException();
			
		if (num < 10) {
			return "00" + num;
		} else if (num >= 10 && num < 100) {
			return "0" + num;
		} else {
			return "" + num;
		}
	}
	
	/**
	 * 判断字符串是否为正整数
	 * 
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static boolean isNumber(String str) throws Exception {

		try {

			for (int i = 0; i < str.length(); i++) {
				if (!Character.isDigit(str.charAt(i))) {
					return false;
				}
			}
			return true;
		} catch (Exception e) {

			e.printStackTrace();
		}
		return false;
	}
	
	/** 
	 * 生成随机字符串 
	 * @param length  字符串长度 
	 * @return 
	 */  
	public static String getRandomString(int length) throws Exception { //length表示生成字符串的长度    
		String base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";       
		Random random = new Random();       
		StringBuffer sb = new StringBuffer();       
		for (int i = 0; i < length; i++) {       
			int number = random.nextInt(base.length());       
			sb.append(base.charAt(number));       
		}       
		return sb.toString();       
	}  
	
	public static void main(String[] args) {
		
		try {
			
			String no = StringTools.getRandomString(30);
			System.out.println(no);
		} catch (Exception e) {

			e.printStackTrace();
		}
	}
}
